package leetcode;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * Created by Ghost on 2014/11/22.
 */
public class Binary_Tree_Level_Order_Traversal {
    public List<List<Integer>> levelOrder(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        LinkedList<List<Integer>> rst = new LinkedList<List<Integer>>();
        if(root==null)
            return rst;
        queue.add(root);
        int count;
        while(!queue.isEmpty()){
            count = queue.size();
            List<Integer> list = new LinkedList<Integer>();
            while(count>0){
                TreeNode cur = queue.poll();
                if(cur.left!=null)
                    queue.add(cur.left);
                if(cur.right!=null)
                    queue.add(cur.right);
                count--;
                list.add(cur.val);
            }
            rst.add(list);
        }
        return rst;
    }
}
